/**
 * Author(s): xuming.Wong <xuming.Wong@gmail.com>
 */
#include "RenderSettingControl.h"

#include <QtGui/QGridLayout>
#include <QtGui/QLabel>
#include <QtGui/QComboBox>
#include <QtGui/QCheckBox>

#include "PQEngine.h"
#include "ColorPicker.h"
#include "NumericTextEdit.h"
#include "BuilderUtil.h"
#include "GameBuilder.h"


namespace PQBuilder
{
	RenderSettingControl::RenderSettingControl(QWidget *parent)
		: QWidget(parent)
	{
		_backgroundColor=ColorConverter::toQt(SEngine.getBackground());
		_fogColor=ColorConverter::toQt(SEngine.getFogColor());
		_fogDensity=SEngine.getFogDensity();

		QGridLayout* grid=new QGridLayout(this);

		QLabel* labelBackground=new QLabel(QObject::tr("Background"),this);
		ColorPicker* pickerBackground=new ColorPicker(this);
		pickerBackground->setColor(_backgroundColor);

		QLabel* labelFogColor=new QLabel(QObject::tr("Fog Color"),this);
		ColorPicker* pickerFogColor=new ColorPicker(this);
		pickerFogColor->setColor(_fogColor);

		QLabel* labelFogDensity=new QLabel(QObject::tr("Fog Density"),this);
		NumericTextEdit* fogDensityEdit=new NumericTextEdit(this);

		QLabel* labelRenderType=new QLabel(QObject::tr("Render Type"),this);
		QComboBox* renderTypeCombo=new QComboBox(this);
		renderTypeCombo->addItem(QObject::tr("Points"));
		renderTypeCombo->addItem(QObject::tr("WireFrame"));
		renderTypeCombo->addItem(QObject::tr("Solid"));
		renderTypeCombo->setCurrentIndex(2);
		
		fogDensityEdit->setFloat(_fogDensity);

		QCheckBox* checkbox=new QCheckBox(QObject::tr("Sky Dome"));
		
		grid->addWidget(labelBackground,0,0);
		grid->addWidget(pickerBackground,0,1);

		grid->addWidget(labelFogColor,1,0);
		grid->addWidget(pickerFogColor,1,1);

		grid->addWidget(labelFogDensity,2,0);
		grid->addWidget(fogDensityEdit,2,1);

		grid->addWidget(labelRenderType,3,0);
		grid->addWidget(renderTypeCombo,3,1);

		grid->addWidget(checkbox,4,0);

		this->setLayout(grid);
		this->setMaximumHeight(grid->rowCount()*28+12);
		connect(pickerBackground,SIGNAL(colorChanged(QColor)),this,SLOT(setBackgroundColor(QColor)));
		connect(renderTypeCombo,SIGNAL(currentIndexChanged(int)),this,SLOT(setRenderType(int)));
		connect(checkbox,SIGNAL(stateChanged(int)),this,SLOT(enableSkyDome(int)));
	}

	void RenderSettingControl::enableSkyDome(int state)
	{
		switch(state){
			case 0:
				SGameBuilder.enableSkyDome(false);
				break;
			case 1:
				break;
			case 2:
				SGameBuilder.enableSkyDome(true);
				break;
		}
	}

	void RenderSettingControl::setBackgroundColor(QColor color)
	{
		SEngine.setBackground(ColorConverter::toOgre(color));
		
	}

	void RenderSettingControl::setRenderType(int index)
	{
		Ogre::PolygonMode mode=(Ogre::PolygonMode)(index+1);
		SEngine.setPolyMode(mode);
		
	}


	RenderSettingControl::~RenderSettingControl()
	{
	}
}
